﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    public class World
    {
        public static float scale = (float)0.4;
        public static int tilesx, tilesy;

        public bool loaded = false;
        private Tile[,] tiles;
        private Camera cam1;

        private bool camLoaded = false;
        //private bool objectsLoaded = false;

        Matrix viewMatrix;
        Matrix projectionMatrix;
        Matrix translateMatrix;

        float przesm = (float)0.1;
        int starex, starey; int jestcontrol; int lpm = 0;

        //Wczytanie świata
        public void Load()
        {
            Tile.LoadMap(@"E:\WindowsGame1\rastert_big1.txt", ref tilesx, ref tilesy, ref tiles);

            foreach (Tile ttile in tiles)
            {
                ttile.SetUpVertices();
                ttile.SetUpIndices();
                ttile.CopyToBuffers();
            }

            cam1 = new Camera();
            camLoaded = true;

            viewMatrix = Matrix.CreateLookAt(cam1.getEye(), cam1.getTarget(), cam1.getUp());
            projectionMatrix = cam1.view();

            loaded = true;
        }

        public void keyUpdate(KeyboardState keyboardState)
        {
            if (keyboardState.IsKeyDown(Keys.W))
            {
                if (jestcontrol == 1) { cam1.moveUp(); }
                else cam1.moveForward();
            }
            if (keyboardState.IsKeyDown(Keys.S))
            {
                if (jestcontrol == 1) { cam1.moveDown(); }
                else cam1.moveBackward();
            }

            if (keyboardState.IsKeyDown(Keys.A))
            {
                cam1.moveLeft();
            }
            if (keyboardState.IsKeyDown(Keys.D))
            {
                cam1.moveRight();
            }

            if (keyboardState.IsKeyDown(Keys.Space))
            {
                jestcontrol = 1;
            }
            else
            {
                jestcontrol = 0;
            }
        }

        public void mouseUpdate(MouseState mouseState)
        {
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                lpm = 1;
            }
            else
            {
                lpm = 0;
            }

            //mysz
            if (lpm == 0) return;
            int x = mouseState.X;
            int y = mouseState.Y;
            if (x < starex)
            {
                starex = x;
                cam1.patrzX(przesm);
            }
            else if (x > starex)
            {
                starex = x;
                cam1.patrzX(-przesm);
            }
            if (y < starey)
            {
                starey = y;
                cam1.patrzY(przesm);
            }
            else if (y > starey)
            {
                starey = y;
                cam1.patrzY(-przesm);
            }
        }

        public void draw()
        {
            if (!loaded) return;

            if (!camLoaded) return;
            Game1.device.Clear(Color.CornflowerBlue);
            viewMatrix = Matrix.CreateLookAt(cam1.getEye(), cam1.getTarget(), cam1.getUp());

            //ustawienia grafiki
            RasterizerState rs = new RasterizerState();
            rs.CullMode = CullMode.None;
            //rs.FillMode = FillMode.WireFrame;
            Game1.device.RasterizerState = rs;

            Game1.effect.Parameters["xView"].SetValue(viewMatrix);
            Game1.effect.Parameters["xProjection"].SetValue(projectionMatrix);
            Game1.effect.Parameters["xWorld"].SetValue(Matrix.Identity);
            Game1.effect.CurrentTechnique = Game1.effect.Techniques["ColoredNoShading"];

            int rtilesx = tilesx; int rtilesy = tilesy;

            //rysowanie swiata
            for (int i = -2; i < 3; i++)
            {
                for (int j = -2; j < 3; j++)
                {
                    if (cam1.aktwx + i < 0 || cam1.aktwz + j < 0 || cam1.aktwx + i >= tilesx || cam1.aktwz + j >= tilesy) continue;

                    translateMatrix = Matrix.CreateTranslation((Tile.width - 1) * World.scale * i, 0, (Tile.height - 1) * World.scale * j);
                    Game1.effect.Parameters["xWorld"].SetValue(Matrix.Identity * translateMatrix);

                    foreach (EffectPass pass in Game1.effect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                        try
                        {
                            tiles[cam1.aktwx+i, cam1.aktwz+j].renderuj(Math.Abs(i)+Math.Abs(j));
                        }
                        catch
                        {

                        }
                    }
                }
            }
            /////////////
        }
    }
}
